package org.glorylight.hacks.social.controller;

import java.text.SimpleDateFormat;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glorylight.hacks.social.data.UserData;
import org.glorylight.hacks.social.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sun.jersey.api.representation.Form;


@Component
@Scope("prototype")
@Path("/signup")
public class SignUpController extends AbstractBaseController {

	private static final Logger log = LoggerFactory.getLogger(SignUpController.class);
	
	@Autowired
	private UserService userService;
	
	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response registrationStep2(Form formData) {
		String password = formData.getFirst("newpassword");
		String birthDate = formData.getFirst("birthDate");
		String gender = formData.getFirst("gender");
		String mobileNo = formData.getFirst("mobileNo");
		String babyName = formData.getFirst("babyName");
		String babyBirthDate = formData.getFirst("babyBirthDate");
		
		UserData userData = userService.find(getActiveLogin());
		SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		userData.setPassword(password);
		userData.setGender(gender);
		userData.setMobileNo(mobileNo);
		userData.setBabyName(babyName);
		userData.setActivate(Boolean.TRUE);
		try {
			userData.setBirthDate(formatter.parse(birthDate));
			userData.setBabyBirthDate(formatter.parse(babyBirthDate));
		} catch (Exception e) {
			log.error("date format is not match", e);
			return Response.status(Response.Status.BAD_REQUEST).build();
		}
		try {
			userService.updateUser(userData);
		} catch (Exception e) {
			log.error("cannot update user", e);
			return Response.status(Response.Status.NOT_ACCEPTABLE).build();
		}
		return Response.status(Response.Status.ACCEPTED).build();
	}
}
